﻿using BCCommon;
using BCEntity.Common.EntityNameConst;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.Common.RedisClear
{
    public class RedisClearService : IRedisClearService
    {
        private readonly IRedisService redisService;

        public RedisClearService(IRedisService redisService)
        {
            this.redisService = redisService;
        }

        public void ClearAdminInfo(long adminId)
        {
            this.redisService.DeleteAsync(string.Format("{0}:AdminId={1}", EntityNameConst.AdminDto, adminId));
        }
        
        public void ClearUser(long userId)
        {
            this.redisService.DeleteAsync(string.Format("{0}:UserId={1}", EntityNameConst.UserEntity, userId));
        }

        public void ClearDriver(long driverId)
        {
            this.redisService.DeleteAsync(string.Format("{0}:DriverId={1}", EntityNameConst.DriverEntity, driverId));
        }


        public void FlushAsync(int? databasesNo)
        {
            this.redisService.FlushAsync(databasesNo);
        }

        public IGrouping<string, KeyValuePair<string, string>>[] Info(string section)
        {
            RedisValue? redisValue = null;
            if (!string.IsNullOrWhiteSpace(section))
            {
                redisValue = section;
            }
            var task = this.redisService.Info(redisValue);
            return task.GetAwaiter().GetResult();
        }

        public string InfoRaw(string section)
        {
            RedisValue? redisValue = null;
            if (!string.IsNullOrWhiteSpace(section))
            {
                redisValue = section;
            }
            var task = this.redisService.InfoRaw(redisValue);
            return task.GetAwaiter().GetResult();
        }
        
        public long Size(int databaseNo = 0)
        {
            return this.redisService.Size(databaseNo).GetAwaiter().GetResult();
        }

        public void RemoveOrderCatch(long orderId, string orderNo)
        {
            string key = string.Format("{0}:OrderId={1}", EntityNameConst.OrderEntity, orderId);
            this.redisService.DeleteAsync(key);
            key = string.Format("{0}:OrderNo={1}", EntityNameConst.OrderEntity, orderNo);
            this.redisService.DeleteAsync(key);
            //key = string.Format("{0}:OrderNo={1}", EntityNameConst.OrderInfoFeeRecordEntity, orderNo);
            //this.redisService.DeleteAsync(key);
            key = string.Format("{0}:OrderNo={1}", EntityNameConst.OrderExtentionEntities, orderNo);
            this.redisService.DeleteAsync(key);
        }
    }
}
